<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\Relatedtags;

/**
 * Controller for rendering page
 * with list of questions for specific tag or tags
 *
 */
class Tagged extends Unanswered
{
	protected $aRequired = array('tags');

	/**
	 * Indicates the current tab
	 *
	 * @var string
	 */
	protected $qtab = 'questions';


	/**
	 * Select items according to conditions passed in GET
	 * Conditions can be == 'unanswered', 'hot', 'recent' (default)
	 */
	protected function getCursor(){

		/**
		 * Must call getTags() before
		 * using this->rawTags because
		 * it is set inside the getTags() method
		 *
		 *
		 */
		$this->aTags = $this->getTags();
		$this->pagerPath = '/tagged/'.$this->rawTags;
		d('aTags: '.print_r($this->aTags, 1));
		
		//$fp = fopen('/var/www/html/qa/www/php.log', 'a+');
		//fwrite($fp, print_r($this->aTags, 1));
		//fclose($fp);
		
		$aFields = array();

		$cond = $this->oRequest->get('cond', 's', 'recent');
		
		/**
		 * Default sort is by timestamp Descending
		 * meaning most recent should be on top
		 *
		 */
		$sort = array('i_ts' => -1);

		$where = array('a_tags' => array('$all' => $this->aTags) );
		$where['i_del_ts'] = null;

		$this->counterTaggedText = \tplCounterblocksub::parse(array(str_replace(' ', ' + ', $this->tags), $this->_('Tagged')), false);

		$this->oCursor = $this->oRegistry->Mongo->QUESTIONS->find($where, $this->aFields);
		$this->count = $this->oCursor->count(true);
		$this->oCursor->sort($sort);

		return $this;
	}



	protected function makeRecentTags(){

		/**
		 * @todo
		 * Currently we are finding related tags for just one tag
		 * This is very fast because we store related tags pre-parsed
		 * as html string. This works and looks good but...
		 * If we really wanted this to work just like
		 * on stackoverflow then we have to change it to this:
		 * if there is only one tag passed in URL, then use this method
		 * otherwise do this:
		 * Use find() on RELATED_TAGS collection and pass $this->aTags
		 * as $all condition. This will find all records in RELATED_TAGS
		 * that have all these tags. Then we have result cursor from which
		 * we are only interested in _id value which is the actual tag related
		 * to all our tags.
		 * We would then loop over cursor and pass
		 * each array to tplRelatedlink, passing each _id
		 * as value of 'tag' and prepend +tag to $this->rawTags as
		 * value of 'link'
		 *
		 * This will be slower than just using pre-parsed related tags
		 * but it will be exactly like on Stackoverflow
		 *
		 * For this to work we need to ensureIndex on 'tags' in RELATED_TAGS
		 * collection!
		 */
		$tag = end($this->aTags);
		d('tag: '.$tag);

		$s = Relatedtags::factory($this->oRegistry)->getHtml($tag);

		if(!empty($s)){
			$tags = \tplBoxrecent::parse(array('tags' => $s, 'title' => $this->_('Related Tags')));
			$this->aPageVars['tags'] = $tags;
		}

		//$this->makeFollowTagButton();

		return $this;
	}

}

